package com.stackfarm.fvservice.service.warning.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.stackfarm.fvcommon.constant.ReturnCode;
import com.stackfarm.fvcommon.pojo.Application;
import com.stackfarm.fvcommon.pojo.Warning;
import com.stackfarm.fvcommon.result.ResultBean;
import com.stackfarm.fvservice.mapper.ApplicationMapper;
import com.stackfarm.fvservice.mapper.WarningMapper;
import com.stackfarm.fvservice.service.warning.WarnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class WarnServiceImpl implements WarnService {
    @Autowired
    private WarningMapper warningMapper;
    @Autowired
    private ApplicationMapper applicationMapper;

    @Override
    public ResultBean<?> setRange(String no, String type, String Min, String Max) {
        ResultBean<Void> resultBean = new ResultBean<>();
        resultBean.setTimestamp(System.currentTimeMillis());
        resultBean.setData(null);
        double min = Double.valueOf(Min);
        double max = Double.valueOf(Max);
        if (min > max) {
            resultBean.setCode(ReturnCode.INVALID_CODE);
            resultBean.setMsg("请输入正确的数值范围");
            return resultBean;
        }
        Application application = applicationMapper.selectOne(new QueryWrapper<Application>()
                .eq("application_id", no)
                .eq("type", type));
        if (application == null) {
            resultBean.setCode(ReturnCode.INVALID_CODE);
            resultBean.setMsg("请输入正确的设备编号及类型");
            return resultBean;
        }
        Warning warning = warningMapper.selectOne(new QueryWrapper<Warning>().eq("application_id", no)
                .eq("type", type));
        if (warning == null) {
            Warning warning1 = new Warning(no, type, min, max);
            warningMapper.insert(warning1);
        } else {
            warning.setMax(max);
            warning.setMin(min);
            warningMapper.update(warning,new QueryWrapper<Warning>().eq("application_id", no)
                    .eq("type", type));
        }
        resultBean.setCode(ReturnCode.SUCCESS);
        resultBean.setMsg("编辑成功");
        return resultBean;
    }
}
